/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.Artist;
import DB.DBmanager;
import javax.swing.JOptionPane;
import GUI.ListArtistForm;
import POJOS.MusicGenre;
import java.util.Date;

/**
 *
 * @author jim
 */
public class NewOrModifyArtistForm extends javax.swing.JFrame {
    Artist artist;
    boolean modify;
    int confirm;
    int comboIndex;
    MusicGenre mc;
    /**
     * Creates new form NewOrModifyArtistForm
     */
    public NewOrModifyArtistForm() {
        initComponents();
        setLocationRelativeTo(null);
        modify = false;
        //Κατά την έναρξη του GUI θέτει την τιμή του ComboBox1 σε null
        jComboBox1.setSelectedItem(null);
       
        
    }
    
    public NewOrModifyArtistForm(Artist a) {
        initComponents();
        modify= true;
        
        artist = a;
        //Αλλάζει τους τίτλους του GUI για την τροποποίηση καλλιτέχνη
        setTitle("ΤΡΟΠΟΠΟΙΗΣΗ ΚΑΛΛΙΤΕΝΗ");
        jLabel1.setText("Παρακαλώ τροποποιείστε τα στοιχεία του καλλιτέχνη:");
        Cancel.setText("ΑΚΥΡΩΣΗ");
       
        //Θέτει στα JText τις τιμές του καλλιτέχνη με τον οποίο κλίθηκε η κλάση
        lastnameJT.setText(artist.getLastname());
        firstnameJT.setText(artist.getFirstname());
        artisticNameJT.setText(artist.getArtisticName());
        sexJT.setText(artist.getSex());
        birthdayJT.setDate(artist.getBirthday());
        birthdayPlaceJT.setText(artist.getBirthPlace());
        jComboBox1.setSelectedItem(artist.getIdMusicGenre().getNameMusicGenre());
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        musicGenreQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT m.nameMusicGenre  FROM MusicGenre m");
        musicGenreList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(musicGenreQuery.getResultList());
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lastnameJT = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        firstnameJT = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        artisticNameJT = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        sexJT = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        Cancel = new javax.swing.JButton();
        Save = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        birthdayPlaceJT = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        birthdayJT = new org.jdesktop.swingx.JXDatePicker();
        jComboBoxSex = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ΕΙΣΑΓΩΓΗ  ΚΑΛΛΙΤΕΧΝΗ");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Εισάγετε τα στοιχεία του νέου καλλιτέχνη:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Επώνυμο:");

        lastnameJT.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Όνομα:");

        firstnameJT.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Καλλιτεχνικό Όνομα:");

        artisticNameJT.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Φύλο:");

        sexJT.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Ημερομηνία Γέννησης:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Είδη Μουσικής:");

        Cancel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        Cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Actions-button-cancel-icon.png"))); // NOI18N
        Cancel.setText("ΕΞΟΔΟΣ");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        Save.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Save-icon.png"))); // NOI18N
        Save.setText("ΑΠΟΘΗΚΕΥΣΗ");
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Τόπος Γέννησης:");

        birthdayPlaceJT.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGenreList, jComboBox1);
        bindingGroup.addBinding(jComboBoxBinding);

        jComboBoxSex.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComboBoxSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Άνδρας", "Γυναίκα", "Male", "Female", " " }));
        jComboBoxSex.setSelectedIndex(-1);
        jComboBoxSex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxSexActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(Save))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(artisticNameJT)
                                    .addComponent(firstnameJT)
                                    .addComponent(lastnameJT, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(birthdayPlaceJT)
                                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(birthdayJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 11, Short.MAX_VALUE))
                                            .addComponent(sexJT))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jComboBoxSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(50, 50, 50))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jLabel1)))
                .addContainerGap(73, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Cancel)
                .addGap(47, 47, 47))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lastnameJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(firstnameJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(artisticNameJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(sexJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(birthdayJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(birthdayPlaceJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 106, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Cancel)
                            .addComponent(Save))
                        .addGap(23, 23, 23))))
        );

        jLabel7.getAccessibleContext().setAccessibleParent(jLabel7);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_CancelActionPerformed

    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
        // TODO add your handling code here:
        //Ελέγχει για υποχρεωτικά πεδία κατά την είσοδο νέου καλλιτέχνη
        if (lastnameJT.getText().isEmpty() || firstnameJT.getText().isEmpty() ||
            artisticNameJT.getText().isEmpty() || sexJT.getText().isEmpty() ||
            birthdayJT.getDate().toString().isEmpty() || birthdayPlaceJT.getText().isEmpty() ||
            jComboBox1.getSelectedItem().toString().isEmpty()){
            JOptionPane.showMessageDialog(null, "Όλα τα πεδία είναι υποχρεωτικά!", "ERROR", JOptionPane.ERROR_MESSAGE);
            } else {
            
            //Για την τροποποίηση καλλιτέχνη
            if (modify == true){
                //Επιβεβαίωση για την τροποποίηση καλλιτέχνη
                confirm = JOptionPane.showConfirmDialog(null, "Επιθυμείτε να αποθηκεύσετε τις αλλαγές;","",JOptionPane.OK_CANCEL_OPTION);
                
                if (confirm == 0){
                    //Θέτει στα στοιχεία του καλλιτέχνη τις τιμές από τα JText
                    artist.setLastname(lastnameJT.getText());
                    artist.setFirstname(firstnameJT.getText());
                    artist.setArtisticName(artisticNameJT.getText());
                    artist.setSex(sexJT.getText());
                    artist.setBirthday(birthdayJT.getDate());
                    artist.setBirthPlace(birthdayPlaceJT.getText());
                    
                    //Επιστρέφει το ID_MusicGenre από το NameMusicGenre
                    mc = DB.DBmanager.identifyMusicGenrewithNameMusicGenre(jComboBox1.getSelectedItem().toString());
                    artist.setIdMusicGenre(mc);
                    
                    //Ενημερώνεται η βάση
                    if (DBmanager.ModifyArtist(artist)){
                        
                        JOptionPane.showMessageDialog(null,"Επιτυχής τροποποίηση καλλιτέχνη!","SUCCESFUL",JOptionPane.INFORMATION_MESSAGE);
                        ListArtistForm.artistList.set(ListArtistForm.artistTable.getSelectedRow(),artist);
                        dispose();
                    }else {
                        JOptionPane.showMessageDialog(null, "Αποτυχία τροποποίησης πελάτη!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    }
                }
                
             // Επιστροφή στην επεξεργασία νέου καλλιτέχνη  
            } else {
                //Επιβεβαίωση για την εισαγωγή
                confirm = JOptionPane.showConfirmDialog(null, "Επιθυμείτε να ολοκληρώσετε την καταχώρηση;","",JOptionPane.OK_CANCEL_OPTION);
                if (confirm == 0){
                    //Αποθήκευση των τιμών των JText σε προσωρινές μεταβλητές                    
                    String newfirstName= firstnameJT.getText();    
                    String newlastName= lastnameJT.getText();
                    String newartisticName= artisticNameJT.getText();
                    String newSex= sexJT.getText();
                    Date newDate= birthdayJT.getDate();
                    String newPlace= birthdayPlaceJT.getText();
                            
                    mc = DB.DBmanager.identifyMusicGenrewithNameMusicGenre(jComboBox1.getSelectedItem().toString());
                    
                    //Δημιουργία νέου καλλιτέχνη
                    artist = new Artist(0,newfirstName,newlastName, newartisticName,newSex,newDate,newPlace,mc);
                     
                    //Αποθήκευση καλλιτέχνη στη βάση
                    if (DB.DBmanager.SaveArtist(artist)){
                        
                        //Πέρασμα καλλιτέχνη στη λίστα
                        ListArtistForm.artistList.add(artist);
                        
                        JOptionPane.showMessageDialog(null,"Επιτυχής καταχώρηση καλλιτέχνη!","SUCCESFUL",JOptionPane.INFORMATION_MESSAGE);
                        
                        //Διαγραφή τιμών από τα JText
                        lastnameJT.setText("");
                        firstnameJT.setText("");
                        artisticNameJT.setText("");
                        sexJT.setText("");
                        birthdayJT.setDate(null);
                        birthdayPlaceJT.setText("");
                        jComboBox1.setSelectedItem(null);
                        
                    }else {
                        //Σε περίπτωση που αποτύχει η αποθήκευση στη βάση
                        JOptionPane.showMessageDialog(null, "Σφάλμα επικοινωνίας με τη ΒΔ!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }       
    }//GEN-LAST:event_SaveActionPerformed

    private void jComboBoxSexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxSexActionPerformed
        // TODO add your handling code here:
        //Περνάει την τιμή από το ComboBoxSex στο sexJT
        sexJT.setText(jComboBoxSex.getSelectedItem().toString());
    }//GEN-LAST:event_jComboBoxSexActionPerformed

    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewOrModifyArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewOrModifyArtistForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.persistence.EntityManager RadioStationPUEntityManager;
    private javax.swing.JButton Save;
    private javax.swing.JTextField artisticNameJT;
    private org.jdesktop.swingx.JXDatePicker birthdayJT;
    private javax.swing.JTextField birthdayPlaceJT;
    private javax.swing.JTextField firstnameJT;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBoxSex;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField lastnameJT;
    private java.util.List<POJOS.MusicGenre> musicGenreList;
    private javax.persistence.Query musicGenreQuery;
    private javax.swing.JTextField sexJT;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
